package com.twu.biblioteca;

import java.io.*;
import java.util.*;

public class BibliotecaApp {

    public HashMap<Integer, MenuOption> getOptionHashMap() {
        return optionHashMap;
    }

    private HashMap<Integer, MenuOption> optionHashMap;
    private UserDatabase userDatabase = new UserDatabase();
    private User user = new User(null, null, null, null, null);
    public static String welcomeMessage = "Welcome to Biblioteca App!";
    public static String menuOptions = "\n1. View All Books.\n2. Reserve a book.\n3. Check Membership Details.\n4. View All Movies.\n5. User Login.\n6. Quit.";
    public static Library library = new Library();
    private static InputReader reader = new InputReader();

    public static void main(String[] args) {
        BibliotecaApp ourApp = new BibliotecaApp();
        ourApp.buildMenu();
        ourApp.constructLibrary();
        ourApp.constructUserDatabase();
        ourApp.printWelcomeMessage();
        do {
            ourApp.printMenuOptions();
            ourApp.handleUserOption(ourApp.getUserOption());
        } while (true);
    }

    public void constructLibrary() {
        library.addBook("Into the Wild");
        library.addBook("The Lord of the Rings");
        library.addBook("On China");
        library.addBook("Headfirst Java");
        library.addBook("Lonely Planet India");
        library.addBook("Wild");
        library.addBook("Under the Banner of Heaven");
        library.addMovie("The Shawshank Redemption", "1994", "Frank Darabont", "9.2");
        library.addMovie("The Godfather", "1972", "Francis Coppola", "9.2");
        library.addMovie("Pulp Fiction", "1994", "Quentin Tarantino", "9.0");
        library.addMovie("Fight Club", "1999", "David Fincher", "8.9");
        library.addMovie("Inception", "2010", "Christopher Nolan", "N/A");
        library.addMovie("Star Wars", "1977", "George Lucas", "8.8");
        library.addMovie("The Matrix", "1999", "Andy Wachowski", "8.7");
        library.addMovie("Psycho", "1960", "Alfred Hitchcock", "8.6");
        library.addMovie("Leon", "1994", "Luc Beson", "8.6");
        library.addMovie("V for Vendetta", "2005", "James McTeigue", "8.2");
    }

    public void constructUserDatabase() {
        userDatabase.addUser("A", "a@gmail.com", "1234567890", "111-1111", "a");
        userDatabase.addUser("B", "b@gmail.com", "1234567891", "111-1112", "b");
    }

    public void buildMenu() {
        optionHashMap = new HashMap<Integer, MenuOption>();
        optionHashMap.put(1, new MenuViewBooks(library));
        optionHashMap.put(2, new MenuReserveBooks(library, reader, user));
        optionHashMap.put(3, new MenuMembership(user));
        optionHashMap.put(4, new MenuViewMovies(library));
        optionHashMap.put(5, new MenuUserLogin(userDatabase, reader, user));
        optionHashMap.put(6, new MenuExit());
    }

    public void printWelcomeMessage() {
        System.out.print(welcomeMessage);
    }

    public static void printMenuOptions() {
        System.out.print(menuOptions);
    }

    public MenuOption getUserOption() {
        String userOption = reader.getUserInput();
        try {
            return optionHashMap.get(Integer.parseInt(userOption));

        } catch (NumberFormatException e) {
            System.out.println("Select a valid option!!");
        }
        return null;
    }

    public void handleUserOption(MenuOption userOption) {
        if (userOption != null)
            userOption.run();
    }

}
